
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import { ILogisticsOrder } from 'src/types/logisticsorder.interface';

type UnionType = ILogisticsOrder | ILogisticsOrder[];

@Injectable()
export class LogisticsorderService {


     /**
     * 参数装饰器,LogisticsOrder 这个名字要和app.module.ts中的MongooseModule.forFeature 定义的名字一样
     * LogisticsOrder 是定义好的
     * @param recommendModel 
     */
     constructor(@InjectModel('LogisticsOrder') private logisticsOrderModel: Model<ILogisticsOrder>) {

     }
 
 
     /**
      * 查询数据
      * @param queryCondition 
      * @returns 
      */
     async findService(queryCondition?:object): Promise<UnionType> {
         return this.logisticsOrderModel.find(queryCondition).exec();
     }
 
 
     /***
      * 向mongodb中插入数据
     */
     async importData(requestParamsBody: UnionType): Promise<UnionType> {
         //如果请求体是一个数组
         if (Array.isArray(requestParamsBody)) {
             return this.logisticsOrderModel.insertMany(requestParamsBody);
         }
         else {
             return new this.logisticsOrderModel(requestParamsBody).save();
         }
     }
 
 
    
    
     async getAllProductInfo(page:number,limit:number):Promise<ILogisticsOrder[]>{
         console.log("page====",page)
 
         const skip = (page-1)* limit;  //类似[].slice(i*limit,(i+1)*limit) 数据的获取
         return this.logisticsOrderModel.find()
         .sort({createdAt:-1})  //按照创建时间倒序排序
         .skip(skip) //跳过指定的条数
         .limit(limit)
         .exec()
         
     }
 
  
     async fetchLogisticsOrderByStatus(page:number,pageSize:number,status:number):Promise<UnionType> {
 
        const skip = (page-1)* pageSize;  //类似[].slice(i*limit,(i+1)*limit) 数据的获取

         const querySelectFileds = ["waybillNum","sourceCity","destCity","pickUpCode","status"];
 
         const data = this.logisticsOrderModel
         .find()
         .sort({createdAt:-1})  //按照创建时间倒序排序
         .skip(skip) //跳过指定的条数
         .where('status')
         .equals(status)
         .select(querySelectFileds)
         .limit(pageSize)
         .exec();
 
         return data;
 
     }
    



}
